<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加角色按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表 -->
      <el-table :data="rolelist" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
              :class="['bdbottom', i1 == 0 ? 'bdtop' : '', 'vcenter']"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag>{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级和三级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, i2) in item1.children"
                  :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']"
                >
                  <el-col :span="5">
                    <el-tag type="success">{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="19">
                    <el-tag
                      :class="[i2 === 0 ? '' : 'bdtop']"
                      v-for="(item3, i3) in item2.children"
                      type="warning"
                      :key="item3.id"
                      closable
                      @close="removeRightById(scope.row, item2.id, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 序号列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightDialogVisible"
      width="50%"
      @close="setRightDialogClosed"
    >
      <el-tree
        node-key="id"
        :data="rolelist"
        :props="defaultProps"
        show-checkbox
        default-expand-all
        :default-checked-keys="defKeys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRightDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Roles",
  data() {
    return {
      rolelist: [
        {
          id: 30,
          roleName: "主管",
          roleDesc: "技术负责人",
          children: [
            // 商品管理
            {
              id: 301,
              path: "goods",
              authName: "商品管理",
              children: [
                {
                  id: 3011,
                  path: "goods",
                  authName: "商品列表",
                  children: [
                    {
                      id: 30111,
                      path: "goods",
                      authName: "添加商品",
                    },
                  ],
                },
                {
                  id: 3012,
                  path: "params",
                  authName: "分类参数",
                  children: [
                    {
                      id: 30121,
                      path: "categories",
                      authName: "获取参数列表",
                    },
                    {
                      id: 30122,
                      path: "categories",
                      authName: "创建参数列表",
                    },
                    {
                      id: 30123,
                      path: "categories",
                      authName: "删除商品参数",
                    },
                  ],
                },
                {
                  id: 3013,
                  path: "categories",
                  authName: "商品分类",
                  children: [
                    {
                      id: 30131,
                      path: "categories",
                      authName: "添加分类",
                    },
                    {
                      id: 30132,
                      path: "categories",
                      authName: "删除分类",
                    },
                    {
                      id: 30133,
                      path: "categories",
                      authName: "获取分类详情",
                    },
                  ],
                },
              ],
            },
            // 订单管理
            {
              id: 302,
              path: "goods",
              authName: "订单管理",
              children: [
                {
                  id: 3021,
                  path: "goods",
                  authName: "订单列表",
                  children: [
                    {
                      id: 30211,
                      path: "goods",
                      authName: "添加订单",
                      children: [],
                    },
                  ],
                },
              ],
            },
            // 权限管理
            {
              id: 303,
              path: "rights",
              authName: "权限管理",
              children: [
                {
                  id: 3031,
                  path: "goods",
                  authName: "角色列表",
                  children: [
                    {
                      id: 30311,
                      path: "goods",
                      authName: "添加角色",
                      children: [],
                    },
                    {
                      id: 30312,
                      path: "goods",
                      authName: "删除角色",
                      children: [],
                    },
                    {
                      id: 30313,
                      path: "goods",
                      authName: "角色授权",
                      children: [],
                    },
                    {
                      id: 30314,
                      path: "goods",
                      authName: "取消角色授权",
                      children: [],
                    },
                    {
                      id: 30315,
                      path: "goods",
                      authName: "获取角色列表",
                      children: [],
                    },
                    {
                      id: 30316,
                      path: "goods",
                      authName: "获取角色详情",
                      children: [],
                    },
                    {
                      id: 30317,
                      path: "goods",
                      authName: "更新角色信息",
                      children: [],
                    },
                    {
                      id: 30318,
                      path: "goods",
                      authName: "更新角色权限",
                      children: [],
                    },
                  ],
                },
                {
                  id: 3032,
                  path: "goods",
                  authName: "权限列表",
                  children: [
                    {
                      id: 30321,
                      path: "goods",
                      authName: "查看权限",
                      children: [],
                    },
                  ],
                },
              ],
            },
            // 用户管理
            {
              id: 304,
              path: "users",
              authName: "用户管理",
              children: [
                {
                  id: 3041,
                  path: "users",
                  authName: "用户列表",
                  children: [
                    {
                      id: 30411,
                      path: "goods",
                      authName: "添加用户",
                      children: [],
                    },
                    {
                      id: 30412,
                      path: "goods",
                      authName: "删除用户",
                      children: [],
                    },
                    {
                      id: 30413,
                      path: "goods",
                      authName: "更新用户",
                      children: [],
                    },
                    {
                      id: 30414,
                      path: "goods",
                      authName: "获取用户详情",
                      children: [],
                    },
                    {
                      id: 30415,
                      path: "goods",
                      authName: "分配用户角色",
                      children: [],
                    },
                  ],
                },
              ],
            },
            // 数据统计
            {
              id: 305,
              path: "params",
              authName: "数据统计",
              children: [
                {
                  id: 3051,
                  path: "goods",
                  authName: "查看数据",
                  children: [],
                },
              ],
            },
          ],
        },
      ],
      setRightDialogVisible: false,
      defaultProps: {
        children: "children",
        label: "authName",
      },
      defKeys: [],
    };
  },
  methods: {
    async removeRightById(scope, id2, id3) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("用户取消了删除");
      }
    },
    showSetRightDialog(row) {
      this.setRightDialogVisible = true;
      this.getLeafKeys(row, this.defKeys);
    },
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => {
        this.getLeafKeys(item, arr);
      });
    },
    setRightDialogClosed() {
      this.defKeys = [];
    },
  },
};
</script>

<style scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #ddd;
}
.bdbottom {
  border-bottom: 1px solid #ddd;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>